import React from 'react';
import dynamic from 'dva/dynamic';
import { getMenuData } from './menu';

// wrapper of dynamic
const dynamicWrapper = (app, models, component) => dynamic({
  app,
  // eslint-disable-next-line no-underscore-dangle
  models: () => models.filter(m => !app._models.some(({ namespace }) => namespace === m)).map(m => import(`../models/${m}.js`)),
  // add routerData prop
  component: () => {
    const p = component();
    return new Promise((resolve, reject) => {
      p.then((Comp) => {
        resolve(props => <Comp {...props} routerData={getRouterData(app)} />);
      }).catch(err => reject(err));
    });
  },
});

async function  getFlatMenuData(menus) {
  let keys = {};
  menus.forEach((item) => {
    if (item.children) {
      keys[item.path] = item.name;
      keys = { ...keys, ...getFlatMenuData(item.children) };
    } else {
      keys[item.path] = item.name;
    }
  });
  return keys;
}

export const getRouterData = (app) => {
  const routerData = {
    '/': {
      component: dynamicWrapper(app, ['user', 'login'], () => import('../layouts/BasicLayout')),
    },
    '/indexHome': {
      component: dynamicWrapper(app, ['msg'], () => import('../routes/Index/Index')),
    },
    '/msg/auditMsg': {
      component: dynamicWrapper(app, ['msg'], () => import('../routes/Msg/AuditMsg')),
    },
    '/msg/playMsg': {
      component: dynamicWrapper(app, ['msg'], () => import('../routes/Msg/PlayMsg')),
    },
    '/msg/msgHis': {
      component: dynamicWrapper(app, ['msg'], () => import('../routes/Msg/MsgHis')),
    },
    '/rdSeekHelpInfo/seekAdd': {
      component: dynamicWrapper(app, ['seekHelp'], () => import('../routes/SeekHelp/SeekAdd')),
    },
    '/rdSeekHelpInfo/seekList': {
      component: dynamicWrapper(app, ['seekHelp'], () => import('../routes/SeekHelp/SeekList')),
    },
    '/follower/followerList': {
      component: dynamicWrapper(app, ['follower'], () => import('../routes/Follower/FollowerList')),
    },
    '/radioStationShow/listRadioStationShow': {
      component: dynamicWrapper(app, ['show','discuss'], () => import('../routes/RadioStationShow/RadioStationShowList')),
    },
    '/systemManage/rdRadioStationInfo': {
      component: dynamicWrapper(app, ['admin','areaRs'], () => import('../routes/System/RadioStationInfo')),
    },
    '/systemManage/adminList': {
      component: dynamicWrapper(app, ['admin'], () => import('../routes/System/AdminList')),
    },
    '/rdSpread/contentList': {
      component: dynamicWrapper(app, ['contentManager'], () => import('../routes/ContentManager/ContentList')),
    },
    '/rdSpread/contentAdd': {
      component: dynamicWrapper(app, ['contentManager'], () => import('../routes/ContentManager/ContentAdd')),
    },
    '/rdActivity/CouponList': {
      component: dynamicWrapper(app, ['coupon'], () => import('../routes/Coupon/CouponList')),
    },
    '/rdActivity/CouponAdd': {
      component: dynamicWrapper(app, ['coupon'], () => import('../routes/Coupon/CouponAdd')),
    },
    '/rdActivity/CouponDetails': {
      component: dynamicWrapper(app, ['coupon'], () => import('../routes/Coupon/CouponDetails')),
    },
    '/rdSpread/advertiseList': {
      component: dynamicWrapper(app, ['contentManager'], () => import('../routes/Shake/ad/AdList')),
    },
    '/rdSpread/AdAdd': {
      component: dynamicWrapper(app, ['contentManager'], () => import('../routes/Shake/ad/AdAdd')),
    },
    '/rdSpread/liveList': {
      component: dynamicWrapper(app, ['contentManager'], () => import('../routes/Shake/live/LiveList')),
    },
    '/rdSpread/liveAdd': {
      component: dynamicWrapper(app, ['contentManager'], () => import('../routes/Shake/live/LiveAdd')),
    },
    '/rdActivity/rdSignup': {
      component: dynamicWrapper(app, ['signup'], () => import('../routes/SignUp/SignUpList')),
    },
    '/rdAutoMenu': {
      component: dynamicWrapper(app, ['autoMenu'], () => import('../routes/autoMenu/RdAutoMenu')),
    },
    '/user/login': {
      component: dynamicWrapper(app, ['login'], () => import('../routes/Login/Login')),
    },
    '/profile/basic': {
      component: dynamicWrapper(app, ['profile'], () => import('../routes/Profile/BasicProfile')),
    },
    '/profile/advanced': {
      component: dynamicWrapper(app, ['profile'], () => import('../routes/Profile/AdvancedProfile')),
    },
    '/result/success': {
      component: dynamicWrapper(app, [], () => import('../routes/Result/Success')),
    },
    '/result/fail': {
      component: dynamicWrapper(app, [], () => import('../routes/Result/Error')),
    },
    '/exception/403': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/403')),
    },
    '/exception/404': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/404')),
    },
    '/exception/500': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/500')),
    },
    '/exception/trigger': {
      component: dynamicWrapper(app, ['error'], () => import('../routes/Exception/triggerException')),
    },
    '/user': {
      component: dynamicWrapper(app, [], () => import('../layouts/UserLayout')),
    },
    '/user/register': {
      component: dynamicWrapper(app, ['register'], () => import('../routes/User/Register')),
    },
    '/user/register-result': {
      component: dynamicWrapper(app, [], () => import('../routes/User/RegisterResult')),
    },
    '/lotteryFunction': {
      component: dynamicWrapper(app, ['lottery'], () => import('../routes/Lottery/HisLottery')),
    },
    '/createNewLottery': {
      component: dynamicWrapper(app, ['lottery'], () => import('../routes/Lottery/CreateNewLottery')),
    },
    // '/lotteryFunction/hisLottery': {
    //   component: dynamicWrapper(app, ['lottery'], () => import('../routes/Lottery/HisLottery')),
    // },
    // '/user/:id': {
    //   component: dynamicWrapper(app, [], () => import('../routes/User/SomeComponent')),
    // },
  };
  // Get name from ./menu.js or just set it in the router data.
  const menuData = getFlatMenuData(getMenuData());
  const routerDataWithName = {};
  Object.keys(routerData).forEach((item) => {
    routerDataWithName[item] = {
      ...routerData[item],
      name: routerData[item].name || menuData[item.replace(/^\//, '')],
    };
  });
  return routerDataWithName;
};
